#include "plot.h"
#include "oled.h"
#include "utils.h"

/* LPC13xx definitions  
             */
/*----------------------------------------------------------------------------
  Thread 1 'ProcessaDados'
 *---------------------------------------------------------------------------*/

osThreadId tid_Plot;
extern uint8_t escala;
extern uint32_t nPontos;
extern osPoolId  mpool;
extern osMessageQId  MsgBox;
extern Coordenada PontoCentral;
bool coordPlot = false;
bool limpaTela = false;

void limparTela()
{
   oled_clearScreen(OLED_COLOR_WHITE);
}
bool desenhaEscala()
{
   char* escalaStr = "";
   switch(escala)
   {
   case 1:
     escalaStr = "10p";
     break;
   case 2:
     escalaStr = "20p";
     break;
   case 3:
     escalaStr = "30p";
     break;
   default:
     return false;
   }
   
   oled_putString(INTERFACE_ESCALA_X + 10, INTERFACE_ESCALA_Y, (uint8_t*)escalaStr , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  
   return true;
}
bool desenhaCoordPCentral()
{
   uint8_t buf[10];
   //oled_fillRect(20,50, 50, 57, OLED_COLOR_WHITE);
   char signal;
   float aux;
   int g,m,s;
   if( PontoCentral.lat >= 0 )
   {
     aux = PontoCentral.lat;
     signal = 'N';
   }
   else{
     aux = -1*PontoCentral.lat;
     signal = 'S'; 
   }
   CoordenadaToStr(aux, &g, &m, &s);
   intToString(g,buf,10,10);
   oled_putString(INTERFACE_CLAT_X,INTERFACE_CLAT_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLAT_X + 15,INTERFACE_CLAT_Y, 'g' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   
   intToString(m,buf,10,10);
   oled_putString(INTERFACE_CLAT_X + 25,INTERFACE_CLAT_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLAT_X + 30,INTERFACE_CLAT_Y, 'm' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   
   intToString(s,buf,10,10);
   oled_putString(INTERFACE_CLAT_X + 35,INTERFACE_CLAT_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLAT_X + 45,INTERFACE_CLAT_Y, 's' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLAT_X + 50,INTERFACE_CLAT_Y, signal , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   
   if( PontoCentral.lon >= 0 )
   {
     aux = PontoCentral.lon;
     signal = 'L';
   }
   else{
     aux = -1*PontoCentral.lon;
     signal = 'O'; 
   }
   
   CoordenadaToStr(aux, &g, &m, &s);
   intToString(g,buf,10,10);
   oled_putString(INTERFACE_CLON_X,     INTERFACE_CLON_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLON_X + 15,INTERFACE_CLON_Y, 'g' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   
   intToString(m,buf,10,10);
   oled_putString(INTERFACE_CLON_X + 20,INTERFACE_CLON_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLON_X + 30,INTERFACE_CLON_Y, 'm' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   
   intToString(s,buf,10,10);
   oled_putString(INTERFACE_CLON_X + 35,INTERFACE_CLON_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLON_X + 45,INTERFACE_CLON_Y, 's' , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
   oled_putChar(INTERFACE_CLON_X + 50,INTERFACE_CLON_Y, signal , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  
   return true;
}
bool desenhaNPontos()
{
   uint8_t buf[10];
   intToString(nPontos, buf, 10, 10);
  // uint8_t size = sizeof(buf)/sizeof(uint8_t);
  // oled_fillRect(INTERFACE_NPONTOS_X + 10 ,INTERFACE_NPONTOS_Y, INTERFACE_NPONTOS_X + 10 + 5*(size), INTERFACE_NPONTOS_Y + 7, OLED_COLOR_WHITE);
   oled_putString(INTERFACE_NPONTOS_X + 10 ,INTERFACE_NPONTOS_Y, buf , OLED_COLOR_BLACK, OLED_COLOR_WHITE);
 
   return true;
}
void plotPonto(uint8_t x, uint8_t y)
{
   oled_putPixel(x,  y, OLED_COLOR_BLACK);
}
void desenhandoVisualizador()
{
  limparTela();
  plotInterface();
  desenhaEscala();
}
void Plot (void const *argument) {
  
    Ponto  *Pptr;
    osEvent  evt;

    oled_init();
    plotTelaInicial();
 
    osSignalWait(FLAG_READY, osWaitForever);
    desenhandoVisualizador();
    osSignalClear(tid_Plot, FLAG_READY);
    for (;;) {
      evt = osMessageGet(MsgBox, 1);
      if (evt.status == osEventMessage) {
        Pptr = evt.value.p;
        if ( !coordPlot )
        {
          coordPlot = true;
          desenhaCoordPCentral();
        }
        plotPonto(Pptr->x, Pptr->y);
        desenhaNPontos();
        osPoolFree(mpool, Pptr);                  // free memory allocated for message
      }
      else if (limpaTela)
      {
        desenhandoVisualizador();
        limpaTela = false;
      }
    }
}
bool plotTelaInicial(){
  
  limparTela();
  oled_putString(35,8,  (uint8_t*)"GPS", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  oled_putString(0,18,  (uint8_t*)"Cibele,Vinicius", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  oled_putString(0,28, (uint8_t*)"Pressione o", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  oled_putString(0,38, (uint8_t*)"botao para", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  oled_putString(0,48, (uint8_t*)"iniciar", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  return true;
}
bool plotInterface(){
  
  //oled_line(0, INTERFACE_BOTTOM_LINE, OLED_DISPLAY_WIDTH, INTERFACE_BOTTOM_LINE, OLED_COLOR_BLACK);
  //oled_putString(INTERFACE_NPONTOS_X,INTERFACE_NPONTOS_Y, (uint8_t*)"N:", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  //oled_putString(INTERFACE_ESCALA_X,INTERFACE_ESCALA_Y,  (uint8_t*)"E:", OLED_COLOR_BLACK, OLED_COLOR_WHITE);
  
  
  return true;
}

